package com.achievo.know.web.component;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import com.sun.xml.internal.rngom.parse.host.Base;
import org.apache.commons.lang.StringUtils;
import org.apache.tools.zip.ZipOutputStream;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ResourceUtils;

import com.achievo.know.application.FileApplication;
import com.achievo.know.component.impl.FileManagerConfig;
import com.achievo.know.domain.FileAttachment;
import com.achievo.know.pojo.FileUtil;
import com.opensymphony.xwork2.ActionSupport;
import sun.misc.BASE64Decoder;

public class DownloadAction extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 577153791960486157L;
	
	private String fileName;
	
	private String id;
	
	private String ids;
	
	private String from;
	
	private String folder;
	
	private String contentType;
	
	@Autowired
	private FileApplication fileApplication;
	@Autowired
    private FileManagerConfig config;


	public InputStream getDownloadFile() throws Exception {  
		if(StringUtils.isNotEmpty(ids)){
			String temppath=config.getZipFileStoreDirectory()+File.separator+UUID.randomUUID().toString()+".zip";
			File file = new File(temppath);
			if(!file.exists()){ 
		        file.createNewFile();
		    }     
			FileOutputStream fous = new FileOutputStream(file); 
			ZipOutputStream zipOut = new ZipOutputStream(fous);
			zipOut.setEncoding("gbk");
			List<File> fileList=new ArrayList<File>();
			List<String> fileNameList=new ArrayList<String>();
			String[] ida=ids.split(",");
			for(String id:ida){
				com.achievo.know.domain.File parent=fileApplication.getFile(Long.parseLong(id));
				if(parent!=null){
					List<FileAttachment> atts=fileApplication.listAttachment(parent.getId(), 1);
					for(FileAttachment at:atts){
						if("temp".equals(folder)){
							String path=config.getTempFileStoreDirectory()+File.separator+at.getAttachPath()+at.getGuid()+"."+at.getAttachType().getName();
							File f=new File(path);
							if(f.exists()){
								fileList.add(f);
								String filename = new String((at.getAttachName()+"."+at.getAttachType().getName()).getBytes());
								fileNameList.add(filename);
							}
						}
					}
					if(StringUtils.isEmpty(this.fileName))
						this.fileName = new String((parent.getFileName()+"等文件"+".zip").getBytes(), "ISO8859-1");
				}
			}
			FileUtil.zipFile(fileList,fileNameList, zipOut);
			zipOut.close();
			fous.close();
			return new FileInputStream(ResourceUtils.getFile(temppath));
		}else{
            BASE64Decoder decoder=new BASE64Decoder();
            String oid=new String(decoder.decodeBuffer(id));
			FileAttachment atts=fileApplication.getAttachment(Long.parseLong(oid));
			if(atts==null){
				throw new RuntimeException("找不到文件!");
			}
			this.fileName = new String((atts.getAttachName()+"."+atts.getAttachType().getName()).getBytes(), "ISO8859-1");
			/*if("1".equals(from)){
				com.achievo.know.domain.File f=fileApplication.getFile(atts.getFkId());
				f.setDownSum(f.getDownSum()+1);
				fileApplication.saveOrUpdateFile(f);
			}*/
			atts.setDownSum(atts.getDownSum()+1);
			fileApplication.saveOrUpdateFileAttachment(atts);
			if("temp".equals(folder)){
				String path=config.getTempFileStoreDirectory()+File.separator+atts.getAttachPath()+atts.getGuid()+"."+atts.getAttachType().getName();
				return new FileInputStream(ResourceUtils.getFile(path));
			}else{
				String path=config.getPersistFileStoreDirectory()+File.separator+atts.getAttachPath()+atts.getGuid()+"."+atts.getAttachType().getName();
				return new FileInputStream(ResourceUtils.getFile(path));
			}
		}
    }  
  
    @Override  
    public String execute() throws Exception {  
        return SUCCESS;  
    }

	public String getFileName() {
		return fileName;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}

	public String getContentType() {
		return contentType;
	}

	public void setContentType(String contentType) {
		this.contentType = contentType;
	}

	public FileApplication getFileApplication() {
		return fileApplication;
	}
	
	

	public String getIds() {
		return ids;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}

	public String getFolder() {
		return folder;
	}

	public void setFolder(String folder) {
		this.folder = folder;
	}

	public void setFileApplication(FileApplication fileApplication) {
		this.fileApplication = fileApplication;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public FileManagerConfig getConfig() {
		return config;
	}

	public void setConfig(FileManagerConfig config) {
		this.config = config;
	}

	public String getFrom() {
		return from;
	}

	public void setFrom(String from) {
		this.from = from;
	}

	
	
    
}
